package com.guigu.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.constant.MessageConstant;
import com.guigu.dao.SetmealMapper;
import com.guigu.entity.PageResult;
import com.guigu.pojo.CheckGroup;
import com.guigu.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Kc
 * @date 2021-04-25
 */
@Service(interfaceClass=SetmealService.class)
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页多条件查询所有的套餐
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> setmeals = setmealMapper.selectByCondition(queryString);
        return new PageResult(setmeals.getTotal(),setmeals.getResult());
    }

    /**
     * 添加套餐
     *
     * @param setmeal
     * @param checkitemIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkitemIds) {
        setmealMapper.add(setmeal);
        setmealAndCheck(setmeal.getId(),checkitemIds);
    }

    /**
     * 查询套餐绑定的检查组选项
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return  setmealMapper.queryByById(id);
    }

    /**
     * 删除关联表信息
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        setmealMapper.deleteAssociation(id);
    }

    /**
     * 删除套餐表 逻辑删除
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteById2(Integer id) {
        int i = setmealMapper.findSetmealCountByCheckGroupId(id);
        if(i>0){
            // 被使用了
            throw new RuntimeException(MessageConstant.DELETE_CHECKITEM_GROUP_FAIL);

        }
        int i1 = setmealMapper.deleteById(id);

        if(i1>0){
            return true;
        }
        return false;
    }

    /**
     * 修改
     *
     * @param setmeal
     * @param checkitemIds
     */
    @Override
    public void edit(Setmeal setmeal, Integer[] checkitemIds) {
        setmealMapper.updateAll(setmeal);
        setmealMapper.deleteAssociation(setmeal.getId());
        setmealAndCheck(setmeal.getId(),checkitemIds);
    }

    /**
     * 查询所有套餐
     * @param id
     * @return
     */
    @Override
    public Setmeal queryAllSetmeal(Integer id) {
        Setmeal setmeal = setmealMapper.queryAllSetmeal(id);
        return setmeal;
    }

    @Override
    public List<CheckGroup> queryAll() {
        return setmealMapper.findAll();
    }

    /**
     * 设置检查组合和检查项的关联关系
     * @param checkitemIds
     */
    private void setmealAndCheck(Integer setmeal_id, Integer[] checkitemIds) {
        if (checkitemIds !=null && checkitemIds.length>0){
            for (Integer checkitemId:checkitemIds) {
                Map<String,Integer> map=new HashMap<String,Integer>();
                map.put("setmeal_id",setmeal_id);
                map.put("checkgroup_id",checkitemId);
                setmealMapper.setmealAndCheck(map);
            }
        }
    }

}
